﻿using System;
using Fine.Utils.Functions;



namespace Crocodile.SimplexLanguage.Parser
{
    /// <summary>
    /// Source line.
    /// </summary>
    /// <stereotype>datum</stereotype>
    public struct SimplexLine
    {
        public readonly int Nr;
        public readonly int Offset;
        public readonly string Str;


        public SimplexLine(int nr, int offset, string str)
        {
            this.Nr = nr;
            this.Offset = offset;
            this.Str = str ?? string.Empty;
        }

        public SimplexLine(int nr, string rawstr)
        {
            this.Nr = nr;
            this.Offset = rawstr.Offset();
            this.Str = Offset == 0 ? rawstr : Offset < Int32.MaxValue ? rawstr.Substring(Offset) : String.Empty;
        }


        public bool IsEmpty()
        {
            return Offset == Int32.MaxValue;
        }


        public bool Equals(SimplexLine other)
        {
            return other.Nr == Nr && other.Offset == Offset && Equals(other.Str, Str);
        }


        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (obj.GetType() != typeof (SimplexLine)) return false;
            return Equals((SimplexLine) obj);
        }


        public override int GetHashCode()
        {
            return Str.GetHashCode();
        }
    }
}